package com.dangs.color;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

    private static String TAG = "ColorAndZoom";
    final float TEXT_SIZE = 20.0f;
    final int PURPLE = 0xFF800080;
    final int YELLOW = Color.YELLOW;
    final int WHITE = Color.WHITE;
    final int BLACK = Color.BLACK;
    final int GREEN = Color.GREEN;
    final int RED = Color.RED;
    final int BACK_COLOR[] = {WHITE, BLACK, YELLOW, PURPLE, GREEN, RED};
    final int TEXT_COLOR[] = {BLACK, WHITE, PURPLE, YELLOW, RED, GREEN};
    final Typeface TYPES[] = {Typeface.DEFAULT, Typeface.DEFAULT_BOLD};
    FrameLayout bg;
    TextView tx, tx_step;
    int index = 0, typeIndex = 0;
    float txSize = TEXT_SIZE;
    int mul = 1;
    int step = 5 + mul * 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bg = findViewById(R.id.bg);
        tx = findViewById(R.id.tx_info);
        tx_step = findViewById(R.id.tx_zoom_step);
        tx.append(getString(R.string.info2));
        tx.append(getString(R.string.info3));
        tx.append(getString(R.string.info4));
        tx.append(getString(R.string.info5));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyDown:" + event);
        switch (keyCode) {
            case KeyEvent.KEYCODE_F6:
                startService(new Intent(this, BackService.class));
//                finish();
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                index++;
                if (index >= BACK_COLOR.length) {
                    index = 0;
                }
                tx.setTextColor(TEXT_COLOR[index]);
                bg.setBackgroundColor(BACK_COLOR[index]);
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                index--;
                if (index < 0) {
                    index = BACK_COLOR.length - 1;
                }
                tx.setTextColor(TEXT_COLOR[index]);
                bg.setBackgroundColor(BACK_COLOR[index]);
                break;
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_MENU:
                txSize = TEXT_SIZE;
                tx.setTextSize(txSize);
                if (tx.getVisibility() == View.VISIBLE) {
                    tx.setVisibility(View.INVISIBLE);
                } else {
                    tx.setVisibility(View.VISIBLE);
                }
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                txSize--;
                if (txSize <= 1) {
                    txSize = 2;
                }
                tx.setTextSize(txSize);
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                txSize++;
                tx.setTextSize(txSize);
                break;
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_P:
            case KeyEvent.KEYCODE_I:
                zoomIn(step);
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_N:
            case KeyEvent.KEYCODE_O:
                zoomOut(step);
                break;
            case KeyEvent.KEYCODE_0:
            case KeyEvent.KEYCODE_1:
            case KeyEvent.KEYCODE_2:
            case KeyEvent.KEYCODE_3:
            case KeyEvent.KEYCODE_4:
            case KeyEvent.KEYCODE_5:
            case KeyEvent.KEYCODE_6:
            case KeyEvent.KEYCODE_7:
            case KeyEvent.KEYCODE_8:
            case KeyEvent.KEYCODE_9:
                mul = keyCode - KeyEvent.KEYCODE_0;
                step = 5 + 5 * mul;
                tx_step.setText(String.valueOf(step));
                break;
        }
        return true;
    }

    private static final String ZOOM_NODE = "/sys/class/projector/LensZoom";

    public void zoomOut(int step) {
        String cmd = "wt 0 " + Integer.toHexString(step) + " 1";
        writeFile(ZOOM_NODE, cmd);
    }

    public void zoomIn(int step) {
        String cmd = "wt 1 " + Integer.toHexString(step) + " 1";
        writeFile(ZOOM_NODE, cmd);
    }

    public static synchronized void writeFile(String filePath, String value) {
        File f = new File(filePath);
        Log.d(TAG, "writeFile:" + value + " -> " + filePath);
        if (!f.exists()) {
            Log.e(TAG, "file not exist!");
        } else {
            try {
                FileOutputStream fOutStream = new FileOutputStream(f);
                fOutStream.write(value.getBytes());
                fOutStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}